﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;

namespace Underrun.Helpers
{
    class EndlessTimer
    {
        private int _noDays;
        public event EventHandler LoopFunction;
        private Timer _timer;
        private AppData appData;

        public EndlessTimer()
        {
            appData = new AppData();
            appData.CheckingType = EndlessTimerType.FirstMondayOfMonth;

            switch (appData.CheckingType)
            {
                case EndlessTimerType.FirstMondayOfWeek:
                    SingleTon.ObjSingle.Task = new Task(SetupEndlessTimerFirstMondayOfWeek);
                    SingleTon.ObjSingle.Task.Start();
                    break;
                case EndlessTimerType.FirstMondayOfMonth:
                    SingleTon.ObjSingle.Task = new Task(SetupEndlessTimerFirstMondayOfMonth);
                    SingleTon.ObjSingle.Task.Start();
                    break;
                case EndlessTimerType.FirstDayOfMonth:
                    SingleTon.ObjSingle.Task = new Task(SetupEndlessTimerFirstDayOfMonth);
                    SingleTon.ObjSingle.Task.Start();
                    break;
            }
        }

        public void SetupEndlessTimerFirstMondayOfWeek()
        {
            if (_timer != null) _timer.Stop();
            _timer = new Timer(5000);
            _timer.Elapsed += new ElapsedEventHandler(TimerElapsedFirstMondayOfWeek);
            _timer.Start();
        }

        public void SetupEndlessTimerFirstMondayOfMonth()
        {
            if (_timer != null) _timer.Stop();
            _timer = new Timer(5000);
            _timer.Elapsed += new ElapsedEventHandler(TimerElapsedFirstMondayOfMonth);
            _timer.Start();
        }

        public void SetupEndlessTimerFirstDayOfMonth()
        {
            if (_timer != null) _timer.Stop();
            _timer = new Timer(5000);
            _timer.Elapsed += new ElapsedEventHandler(TimerElapsedTimerFirstDayOfMonth);
            _timer.Start();
        }

        void TimerElapsedFirstMondayOfWeek(object sender, ElapsedEventArgs e)
        {
            System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "_FirstMondayOfWeek" + SingleTon.ObjSingle.Temp.ToString() + ".txt");
        }

        void TimerElapsedFirstMondayOfMonth(object sender, ElapsedEventArgs e)
        {
            System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "_FirstMondayOfMonth" + SingleTon.ObjSingle.Temp.ToString() + ".txt");
        }

        void TimerElapsedTimerFirstDayOfMonth(object sender, ElapsedEventArgs e)
        {
            System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "_FirstDayOfMonth" + SingleTon.ObjSingle.Temp.ToString() + ".txt");
        }
    }
}
